#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'tian'
__data__ = '2022/5/16 12:23'
# software: PyCharm

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# author： 青城子
# datetime： 2022/4/13 21:12
# ide： PyCharm

"""
信号量实现点击数+1

"""

from django.db.models.signals import post_save,post_delete
from django.dispatch import receiver
from django.contrib.auth import get_user_model

from .models import UserFav

User = get_user_model()


@receiver(post_save, sender=UserFav)  # sender=User接收UserFav模型传递过来的
def create_user_fav(sender, instance=None, created=False, **kwargs):
    if created:  # 新增时created=True,实现了收藏数+1操作
        goods = instance.goods
        goods.fav_num += 1
        goods.save()


# 用户取消收藏-1
@receiver(post_delete, sender=UserFav)  # sender=User接收UserFav模型传递过来的
def delete_user_fav(sender, instance=None, created=False, **kwargs):
    goods = instance.goods
    goods.fav_num -= 1
    goods.save()